package com.TestDemo.Test202203To04;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zjz
 * @Date: 2022/03/28/15:09
 * @Description:
 * @Target: 链表插入方式测试
 */
public class LinkedNodeAddMethodTest {

    public static void main(String[] args) {
        LinkedNode headNode = new LinkedNode();
        LinkedNode headNode1 = new LinkedNode();


        // 头插法，倒序进数
        headNode.value = 5;
        for (int i = 0; i < 5; i++) {
            LinkedNode newLinkedNode = new LinkedNode();
            newLinkedNode.value = i;
            newLinkedNode.next = headNode.next;
            headNode.next = newLinkedNode;
        }



        // 顺序插入法
        LinkedNode tempNode = new LinkedNode();
        headNode1.value = 0;
        tempNode = headNode1;
        for (int i = 1; i < 5; i++) {
            LinkedNode newLinkedNode = new LinkedNode();
            newLinkedNode.value = i;
            tempNode.next = newLinkedNode;
            tempNode = newLinkedNode;
        }



        // Out
        while (headNode1!=null){
            System.out.print(headNode1.value+" ");
            headNode1 = headNode1.next;
        }

    }


    static class LinkedNode{
       private int value;
       private LinkedNode next;

        public LinkedNode() {
        }

        public LinkedNode(int value, LinkedNode next) {
            this.value = value;
            this.next = next;
        }
    }
}
